/*
  Part of the MasterTool3D project - http://code.google.com/p/master-tool-3d

  Copyright (c) 2011 Ingo Pueschl

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation, version 2.1.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package techjit.gfx.util;

import processing.core.PApplet;
import processing.core.PConstants;
import techjit.gfx.util.geo.Vector;
import techjit.util.RuntimeParams;

public class ConeDrawer {

	// FIELDS
	private static int CONE_DETAIL = RuntimeParams.CONE_DRAWER__CONE_DETAIL;

	// METHODS
	public static void draw(PApplet p, float length, float diameter) {
		float radius = diameter / 2;
		float angleInc = 360 / CONE_DETAIL;

		// CONE_DETAIL many triangles on cone side
		for (int i = 1; i <= CONE_DETAIL; i++) {
			p.pushMatrix();
			{
				// current and previous partial angle
				float alpha = i * angleInc;
				float previousAlpha = alpha - angleInc;

				p.beginShape(PConstants.TRIANGLES);
				{
					// previous vertex on point base circle
					PU.vertex(
							p,
							new Vector(0, PApplet.sin(PApplet
									.radians(previousAlpha)) * radius, -PApplet
									.cos(PApplet.radians(previousAlpha))
									* radius));
					// current vertex on point base circle
					PU.vertex(
							p,
							new Vector(0, PApplet.sin(PApplet.radians(alpha))
									* radius, -PApplet.cos(PApplet
									.radians(alpha)) * radius));
					// vertex at cone tip
					PU.vertex(p, new Vector(length, 0, 0));
				}
				p.endShape();

			}
			p.popMatrix();

		}
	}
}
